package cn.com.b2office.struts2.interceptor;

import java.net.URLEncoder;
import java.util.Date;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class RedirectLoginInterceptor extends AbstractInterceptor {

	private static final long serialVersionUID = -7709954975469867923L;

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		String period_day = null;
		Date cur = new Date();
		int hours = cur.getHours();
		if (hours >= 5 && hours < 9)
			period_day = "早上";
		else if (hours >= 9 && hours < 11)
			period_day = "上午";
		else if (hours >= 11 && hours < 14)
			period_day = "中午";
		else if (hours >= 14 && hours < 18)
			period_day = "下午";
		else if (hours >= 18 && hours < 24)
			period_day = "晚上";
		else if (hours >= 0 && hours < 5)
			period_day = "凌晨";

		invocation.getStack().set("period_day", period_day);

		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		StringBuffer url = request.getRequestURL();
		if (request.getQueryString() != null)
			url.append("?").append(request.getQueryString());
		String value = url.toString();
		if (value.contains("login") || value.contains("Login")) {
			
		} else {
			Cookie cookie = new Cookie("requestURL", URLEncoder.encode(value,
					"utf-8"));
			response.addCookie(cookie);
		}
		String resultCode = invocation.invoke();
		return resultCode;
	}
}
